// home.js
import base64 from '../../utils/base64.js'
const util = require('../../utils/util.js')
const app = getApp()

Page({
  data: {
    userInfo: '',
    hasUserInfo: false,
    iconTitle: ['扫一扫', '咨询', '会员码', '卡包'],
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640', 
    ],
    indicatorColor: 'red',
    indicatorActiveColor: '#fff',
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    circular: false,
    interval: 2000, 
    duration: 500,
    previousMargin: 0,
    nextMargin: 0
  },

  onShareAppMessage () {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  onLoad: function () {
    // this.setData({
    //   logs: (wx.getStorageSync('logs') || []).map(log => {
    //     return util.formatTime(new Date(log))
    //   })
    // })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        systemInfo: app.globalData.systemInfo,
        OPENIDTOKEN: app.globalData.OPENIDTOKEN,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  onShow () {
    // this.getTabBar().setData({ active: 0 })
    // this.getTabBar().init();
  },

  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },

  //* ** 微信调用设二维码 */
  handlescanCode: function (e) {
    wx.scanCode({
      success (res) {
        console.log(res, '微信调用设二维码')
        var decodeString = base64.decode(res.rawData) // 解码
        console.log(decodeString, 'base64中文字符测试') // base64中文字符测试
        wx.navigateTo({
          url: decodeString
        })
      }
    })
  },

  //* ** 获取用户登录 */
  getUserInfo: function (e) {
    const detail = e.detail
    console.log(e, '正在测试getUserInfo')
    if (detail.errMsg == 'getUserInfo:ok' || detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  }
})
